﻿using Furion.Schedule;
using JQData.Application.DataFetch;
using JQData.Core.DbModel;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JQData.Application.Jobs
{
    public class RefreshCache : IJob
    {

        private readonly ILogger<RefreshCache> _logger;
        private readonly IServiceScopeFactory _scopeFactory;
        public RefreshCache(ILogger<RefreshCache> logger, IServiceScopeFactory scopeFactory)
        {
            _logger = logger;
            _scopeFactory = scopeFactory;

        }

        public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
        {
            _logger.LogInformation($"{context}");
            using var serviceScope = _scopeFactory.CreateScope();
            var cache = serviceScope.ServiceProvider.GetService<DataCacheManage>();
            try
            {
                await cache.RefreshCache();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "刷新缓存失败");
            }

        }
    }
}
